﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

namespace Geek7Utils
{
    class Program
    {

        public static SimpleHttp httpClient = null;
        public static SettingData settingData = null;
        public static Queue<string> tempFileQueue = new Queue<string>();

        static void Main(string[] args)
        {

            int maxThreadNum = 10;
            int port = 8081;

            //初始化配置
            InitConf();

            //打印提示
            Console.WriteLine($"base url: http://www.geek7.ltd\r\nclean interval: {settingData.clean_interval}\r\nclean threshold: {settingData.clean_threshold}\r\n\r\nHttp Server Info:\r\n\tMax Thread Count: {maxThreadNum}\r\n\tPort: {port}\r\n");
            Console.Title = "Http Server - Quit Window By Perss \"Enter\"";

            //创建Http服务器
            httpClient = new SimpleHttp(maxThreadNum);

            //添加路由
            httpClient.AddRouter("BuildPlayable", new CocosTool(httpClient, settingData).BuildPlayable244);

            //启动Http服务器
            httpClient.Start(port);

            //防止控制台退出
            Console.WriteLine("Quit Window By Perss \"Enter\" .");

            //启动定时器
            System.Threading.Timer mytimer = new System.Threading.Timer(new System.Threading.TimerCallback(Update), null, 0, 1000 * settingData.clean_interval);

            //任意键退出服务器
            Console.ReadKey();

            //释放临时文件缓存
            ReleaseTempFolder();
        }


        //处理逻辑
        public static void Update(object a)
        {
            //小于清理阈值不做清理操作
            if (tempFileQueue.Count < settingData.clean_threshold)
                return;

            //每次清理 略过最近一次处理的文件 避免冲突
            for (int i = 0; i < tempFileQueue.Count - 1; i++)
            {
                string path = tempFileQueue.Dequeue();
                if (Directory.Exists(path))
                {
                    Directory.Delete(path,true);
                }
                else if( File.Exists(path) )
                {
                    File.Delete(path);
                }
            }
        }


        //初始化临时文件夹
        public static void InitConf()
        {
            try
            {

                string json = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(), "conf\\setting.json"));
                settingData = JsonConvert.DeserializeObject<SettingData>(json);

                var tempDir = settingData.outpath;
                if (!Directory.Exists(tempDir))
                    Directory.CreateDirectory(tempDir);
                Console.WriteLine($"TempDir: {tempDir}");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }


        //释放临时文件夹
        public static void ReleaseTempFolder()
        {
            if (Directory.Exists(settingData.outpath))
                Directory.Delete(settingData.outpath, true);
        }
    }



    [Serializable]
    class SettingData
    {
        public string outpath = "";//使出文件路径
        public string base_url = "http://www.geek7.ltd/playableFile";//基础文件夹地址
        public int clean_interval;//清理间隔 秒
        public int clean_threshold;//小于等于当前文件数量时 本次不清理
    }
}
